package com.lww.feature.test.jndi;

import org.junit.Test;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import java.util.Hashtable;

/**
 * User: yian
 * Date: 2017/9/1
 */
public class test {
    @Test
    public void naming() {
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.fscontext.RefFSContextFactory");//该类不存在这是jdk1.2的例子
        try {
            Context ctx = new InitialContext(env);
            Object obj = ctx.lookup("/Users");
            System.out.println("/Users" + " is bound to: " + obj);

        } catch (NamingException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void directory() {
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

        try {
            DirContext ctx = new InitialDirContext(env);

            Attributes attrs = ctx.getAttributes("cn = Ted Geisel, ou=People");
            System.out.println("sn: " + attrs.get("sn").get());
        } catch (NamingException e) {
            System.err.println("Problem getting attribute:" + e);
        }

    }
    @Test
    public void defaults() {
        try {
            InitialDirContext initialDirContext = new InitialDirContext();
            Object denv = initialDirContext.lookup("java:comp/env");
            System.out.println("java:comp/env"+denv);

        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
}
